package com.ym.config;

import feign.Response;
import feign.RetryableException;
import feign.codec.ErrorDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class FeignConfig {

    @Bean
    public ErrorDecoder errorDecoder() {
        return new ErrorDecoder() {
            private final ErrorDecoder defaultDecoder = new Default();

            @Override
            public Exception decode(String methodKey, Response response) {
                // 如果是 5xx 错误，转为可重试异常
                if (response.status() >= 500) {
                    return new RetryableException(
                            response.status(),
                            "Server Error: " + response.status(),
                            response.request().httpMethod(),
                            new Date(),
                            response.request()
                    );
                }
                // 其他情况（如 4xx）使用默认处理
                return defaultDecoder.decode(methodKey, response);
            }
        };
    }
}
